﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StreetsOfRageStyleGame.Enums;

namespace StreetsOfRageStyleGame
{
    class PlayerCharacter : CharacterSprite
    {
        public PlayerCharacter() { }

        public PlayerCharacter(GraphicsDeviceManager graphicsDeviceManager, Character character, CharacterAlignment characterAlignment, int characterHitPoints)
        {   
            this.graphicsDeviceManager = graphicsDeviceManager;
            this.characterHitPoints = characterHitPoints;
            this.character = character;
            this.characterAlignment = characterAlignment;
        }

        public void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();

            mPreviousKeyboardState = aCurrentKeyboardState;

            //Increase the timer by the number of milliseconds since update was last called
            timer += (float)theGameTime.ElapsedGameTime.TotalMilliseconds;

            //Check the timer is more than the chosen interval


            if (timer > interval)
            {
                currentFrame = GetNextFrame();
                //Reset the timer
                timer = 0f;
            }

            ticksSinceLastKeyPress++;

            //If we are on the last frame, reset back to the one before the first frame (because currentFrame++ is called next so the next frame will be 1!)

            Position += mDirection * mSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
            UpdateMovement(aCurrentKeyboardState);
        }

        private void UpdateMovement(KeyboardState aCurrentKeyboardState)
        {

            mSpeed = Vector2.Zero;
            mDirection = Vector2.Zero;

            int MaxX = graphicsDeviceManager.GraphicsDevice.Viewport.Width - spriteSheet1[currentFrame].Width;
            int MinX = 0;
            int MaxY = graphicsDeviceManager.GraphicsDevice.Viewport.Height - spriteSheet1[currentFrame].Height;
            int MinY = 0;

            if (aCurrentKeyboardState.IsKeyDown(Keys.OemPeriod))
            {
                if (lastKeyPressed != Keys.OemPeriod)
                {
                    currentFrameNumber = 1;
                }
                lastKeyPressed = Keys.OemPeriod;
                ticksSinceLastKeyPress = 0;
                ChangeCharacterState(CharacterState.Punch);
            }
            else if (aCurrentKeyboardState.IsKeyDown(Keys.A) == true)
            {
                lastKeyPressed = Keys.A;
                ticksSinceLastKeyPress = 0;
                //move left
                if (Position.X >= MinX)
                {
                    mSpeed = new Vector2(CHARACTER_SPEED, mSpeed.Y);
                    mDirection = new Vector2(MOVE_LEFT, mDirection.Y);

                    currentDirection = CharacterDirection.Left;
                    ChangeCharacterState(CharacterState.Walking);
                }
                else
                {
                    ChangeCharacterState(CharacterState.Still);
                }
            }
            else if (aCurrentKeyboardState.IsKeyDown(Keys.D) == true)
            {
                lastKeyPressed = Keys.D;
                ticksSinceLastKeyPress = 0;
                if (Position.X <= MaxX)
                {
                    mSpeed = new Vector2(CHARACTER_SPEED, mSpeed.Y);
                    mDirection = new Vector2(MOVE_RIGHT, mDirection.Y);

                    currentDirection = CharacterDirection.Right;
                    ChangeCharacterState(CharacterState.Walking);
                }
                else
                {
                    ChangeCharacterState(CharacterState.Still);
                }
            }
            else if (aCurrentKeyboardState.IsKeyDown(Keys.W) == true)
            {
                lastKeyPressed = Keys.W;
                ticksSinceLastKeyPress = 0;

                mSpeed = new Vector2(mSpeed.X, CHARACTER_SPEED);
                mDirection = new Vector2(mDirection.X, MOVE_UP);

                ChangeCharacterState(CharacterState.Walking);
            }
            else if (aCurrentKeyboardState.IsKeyDown(Keys.S) == true)
            {
                lastKeyPressed = Keys.S;
                ticksSinceLastKeyPress = 0;

                mSpeed = new Vector2(mSpeed.X, CHARACTER_SPEED);
                mDirection = new Vector2(mDirection.X, MOVE_DOWN);

                ChangeCharacterState(CharacterState.Walking);
            }
            else
            {
                lastKeyPressed = Keys.None;
                ticksSinceLastKeyPress = 0;

                mSpeed = new Vector2(0, 0);

                ChangeCharacterState(CharacterState.Still);
            }
        }
    }
}
